// pages/next/next.js
const app = getApp();
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url:''
    // url:'cloud://hjkb-dw89p.686a-hjkb-dw89p-1256153756/bookcover/1.png'
  },

  SetShadow(e) {
    this.setData({
      shadow: e.detail.value
    })
  },
  SetBorderSize(e) {
    this.setData({
      bordersize: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

    that.setData({
      bookid: options.id,
      article:1,
      totalnum:50,
      percent:1
    })

    console.log(this.data.totalnum)
   
    setTimeout(function() {
      that.setData({
        loading: true,
      })
    }, 500)

    // 在这里要拿到书本的第一页放在上面
    // if(options.id==1){
   
      let book='book19962020_'+options.id;
      console.log('table',book)
      db.collection(book).where({
         // 填入当前用户 openid
      }).count({
        success: function(res) {
          console.log(res.total)
          
          // 查询数据长度并且更新到总数
          that.setData({
            totalnum:res.total,
          })

        },
        fail: console.error
      })


      // if(options.id==1){
        db.collection(book).doc('1').get({
          success: function(res) {
            // res.data 包含该记录的数据
            console.log('启动时请求的数据',res.data)
            that.setData({
              url:res.data.url,
            })
              
          }
        })
      // }

      console.log('查看肤质情况',this.data.url)


      
    // }


  },
  last(e){

    let articleid=this.data.article-1
    
    let tm=this.data.totalnum

    let that=this


    if(articleid<1){
      // console.log('第一页');
      wx.showToast({
          title: '已经是第一页',
          icon: 'success',
          duration: 2000
      })


      return;
    }


    // 渲染数据
    // if(this.data.bookid==1){

      let _id=JSON.stringify(articleid)
      // console.log('nnnnnniii上一页赋值开始')
      let book='book19962020_'+that.data.bookid;

      db.collection(book).doc(_id).get({
        success: function(res) {
          // res.data 包含该记录的数据
          console.log('上一页赋值',res.data)
          that.setData({
            url:res.data.url,
          })
            
        },fail(res){
            console.log('你好',res)
        }
      })
    // }

    console.log('查看取值情况',this.data.url)

    let ls=  100*articleid/tm
    let newpercent=ls.toFixed(2);

    this.setData({
      article: articleid,
      percent: newpercent,
    })
    console.log(this.data.article);

  },
  next(e){


    let articleid=this.data.article+1
    let tm=this.data.totalnum
    let that=this

    if(articleid>tm){
      // console.log('最后一页了')
      wx.showToast({
          title: '最后一页了',
          icon: 'success',
          duration: 2000
      })
      return;
    }



    // 渲染数据
    // if(this.data.bookid==1){

      let _id=JSON.stringify(articleid)
      let book='book19962020_'+that.data.bookid;

      // console.log('下一页赋值开始')
      db.collection(book).doc(_id).get({
        success: function(res) {
          // res.data 包含该记录的数据
          console.log('下一页赋值',res.data)
          that.setData({
            url:res.data.url,
          })
            
        },fail(res){
          console.log(res)
        }
      })

      
    // }
    console.log('查看取值情况',this.data.url)


    let ls=  100*articleid/tm
    let newpercent=ls.toFixed(2);


    this.setData({
      article: articleid,
      percent: newpercent,
    })

    console.log(this.data.article);
  },

  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  SetColor(e) {
    this.setData({
      color: e.currentTarget.dataset.color,
      modalName: null
    })
  },
  SetActive(e) {
    this.setData({
      active: e.detail.value
    })
  }
})